<?php
/*
*	Kleophatra - Open Source Content Management System
*	------------------------------------------------------------------------
*	Copyright (C) 2007-2010, Sergio Milardovich <smilardovich@yahoo.com.ar>
*	This program is Free Software.
*
*	@package	Kleophatra
*	@subpackage	/lib
*	@license	http://www.gnu.org/copyleft/gpl.html GNU/GPL License 2.0
*	@author		Sergio Milardovich <smilardovich@yahoo.com.ar>
*	@link		http://www.kleophatra.org
*/
class kleomenu extends kleotemplate{
	var $menu_item;
	function load_menu($menu,&$output,$parent=0,$orderby='iorder ASC'){
		$output;
		$menu_query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."menus WHERE mid = $menu OR name = '$menu' LIMIT 1");
		$menu_row = $this->fetch_array($menu_query);
		$menuid = (int)$menu_row['mid'];

		$query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."menus_items WHERE menuid = $menuid AND parent = $parent ORDER BY $orderby");

		$i=0;
		while($row = $this->fetch_array($query)){
			$output['items'][$i] = $row;
			if($row['link'] == $_SERVER['PHP_SELF'] or $row['link'] == '?'.$_SERVER['QUERY_STRING'] or $row['link'] == $_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'] ){
				$output['items'][$i]['class'] = $menu_row['items_selected_class'];
			} else {
				if($row['link'] == 'home'){
					$output['items'][$i]['link'] = 'index.php';
					if(KLEO_ACTUAL_MODULE == KLEO_IS_HOME && !isset($_GET['module'])){
						$output['items'][$i]['class'] = $menu_row['items_selected_class'];
					}
				} else if(preg_match("/(.*)\?(.*)module\=".KLEO_ACTUAL_MODULE."(.*)/", $row['link'], $matches) && !isset($output['items'][$i]['class'])){
					$cuts = explode("&", $matches[0]);
					foreach($cuts as $cut){
						if(!isset($found)){
							$output['items'][$i]['class'] = $menu_row['items_class'];
							if(isset($_GET[$cut])){
								$found = true;
								$output['items'][$i]['class'] = $menu_row['items_selected_class'];
							}
						} else {
							$output['items'][$i]['class'] = $menu_row['items_class'];
						}
					}
				} else {
					$output['items'][$i]['class'] = $menu_row['items_class'];
				}
			}
			$cparent = $row['mitid'];
			$childs_query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."menus_items WHERE menuid = $menuid AND parent = $cparent ORDER BY iorder ASC");
			$childs="<ul>";
			if($this->num_rows($childs_query) !== 0){
				while($childs_row = $this->fetch_array($childs_query)){
					$childs .= '<a href="'.$childs_row['link'].'"><li>'.$childs_row['title'].'</li></a>';
				}
				$childs .= "</ul>";
				$output['items'][$i]['childs'] = $childs;
			}
			$i++;
		}
	}
	function get_menu_name($id){
		$menu_name_query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."menus WHERE mid = $id");
		$menu_name_row = $this->fetch_array($menu_name_query);
		return $menu_name_row['name'];
	}
	function item_has_childs($itid){
		$query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."menus_items WHERE mitid = $itid");
		if($this->num_rows($query) !== 0){
			return true;
		} else {
			return false;
		}
	}
	function get_menu_from_item($id){
		$query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."menus_items WHERE mitid = $id");
		$row = $this->fetch_array($query);
		return (int)$row['mid'];
	}
	function delete_menu_item($id){
		$this->query("DELETE FROM `".KLEO_TABLE_PREFIX."menus_items` WHERE `mitid` = $id LIMIT 1");
	}
	function add_menu_item($name,$mid,$desc,$link,$parent){
		$this->query("INSERT INTO `".KLEO_TABLE_PREFIX."menus_items` (
			`mitid` ,
			`menuid` ,
			`title` ,
			`desc` ,
			`icon` ,
			`link` ,
			`parent` ,
			`iorder`
		) VALUES (
			NULL , '$mid', '$name', '$desc', 'default.png', '$link', '$parent', '1'
		);");
	}
	function edit_menu_item($id,$mid,$name,$desc,$link,$parent){
	  $this->query("UPDATE `".KLEO_TABLE_PREFIX."menus_items` SET `menuid` = '$mid',
	    `title` = '$name',
	    `desc` = '$desc',
	    `link` = '$link',
	    `parent` = '$parent'
	   WHERE `".KLEO_TABLE_PREFIX."menus_items`.`mitid` =$id LIMIT 1 ;");
	}
	function get_menu_parent($menuid){
	  $query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."menus_items WHERE menuid = $menuid");
	  $row = $this->fetch_array($query);
	  return (int)$row['parent'];
	}
	function get_item_parent($itid){
	  $query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."menus_items WHERE mitid = $itid");
	  $row = $this->fetch_array($query);
	  return (int)$row['parent'];
	}

}

?>
